<?php

//Loader::loadClass('SecurityUtilEx', "modules/VotePetition/pnincludes");

/**
* //////////////////////////////////////////////////
* auto execute , for initialize config variable
* this function auto call every page has been fetch
*/
function _autoexecute(){

}

function VotePetition_user_permission()
{
  // Security check
  //we are allow for admin access level , see in config.php variable name ACCESS_EDIT
  //if (!SecurityUtil::checkPermission('VotePetition::', '::', ACCESS_EDIT)) {
  //    LogUtil::registerPermissionError(pnModUrl('Users','user','loginscreen'));
  //}
}

/**
* smarty template auto call before render
*/
function _preRender(&$render){
  $lang    = FormUtil::getPassedValue ('lang', false , 'GET');
  $ctrl    = FormUtil::getPassedValue ('ctrl', false , 'GET');

  $render->assign ('_GET', $_GET);
  $render->assign ('_POST', $_POST);
  $render->assign ('_REQUEST', $_REQUEST);

  $render->assign('ctrl', $ctrl);
  //$render->assign('setting',InnoUtil::getSetting());
  $render->assign('user',   InnoUtil::getUserInfo());

  if ($lang){
    $render->assign('lang', $lang);
  }else{
    $render->assign('lang', pnUserGetLang());
  }
  $render->assign('access_edit', SecurityUtil::checkPermission('VotePetition::', '::', ACCESS_EDIT));
}

/**
 * Main user function . the user side page controller
 * @author Chayakon PONGSIRI
 * @return string HTML string
 */
function VotePetition_user_main() {
    //_autoexecute();
    return VotePetition_xml_list();
}


function VotePetition_xml_list()
{  
  
  $pid  = FormUtil::getPassedValue ('pid', null , 'GET');
  $did  = FormUtil::getPassedValue ('did', null , 'GET');
  $tid  = FormUtil::getPassedValue ('tid', null , 'GET');
  $vid  = FormUtil::getPassedValue ('vid', null , 'GET');

  //pagnigation variable
  $sort       = FormUtil::getPassedValue ('sort', null , 'GET');
  $pagesize   = FormUtil::getPassedValue ('pagesize', false , 'GET') ? FormUtil::getPassedValue ('pagesize', false , 'GET') : 200;
  $page       = FormUtil::getPassedValue ('page', null , 'GET');
     
  $render = pnRender::getInstance('VotePetition');

  if (!($class = Loader::loadClassFromModule ('VotePetition','VillagePoll', true)))
    return LogUtil::registerError ("Unable to load class [VillagePoll] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  if(isset($par_id)||isset($pid)||isset($did)||isset($tid)||isset($vid)){
    if (method_exists($objectArray,'genFilter')){
      $where = $objectArray->genFilter();
      $sort = $objectArray->genSort();
    }
    if (method_exists($objectArray,'genSort')){
      $sort = $objectArray->genSort();
    }
  }

  if (empty($where)){
    $where = implode(' AND ',$objectArray->_objWhere);
  }else if (is_array($objectArray->_objWhere)){
    $where .= implode(' AND ',$objectArray->_objWhere);
  }else if (!empty($objectArray->_objWhere)){
    $where .= ' AND ' . $objectArray->_objWhere;
  }
  if (empty($sort)){
    $sort = $objectArray->_objSort;
  }


  //Spilt page
  $pager = array();
  $totalItems  = $objectArray->getCount($where , true);
  $totalPages  = ceil($totalItems/$pagesize);
  $nowPage = $page;
  if($page>0){
    $page--;
  }
  $page = $page*$pagesize;
  $objectArray->get($where, $sort , $page, $pagesize);
  $objDataArray = $objectArray->_objData;
  
  if(intval($par_id)&&isset($par_id))
  {
    $args = "&par_id=".$par_id;
  }
  if(intval($pid)&&isset($pid))
  {
    $args = "&pid=".$pid;
  }
  if(intval($did)&&isset($did))
  {
    $args = "&did=".$did;
  }
  if(intval($tid)&&isset($tid))
  {
    $args = "&tid=".$tid;
  }
  if(intval($vid)&&isset($vid))
  {
    $args = "&vid=".$vid;
  }
  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;

  
  $xmlChannel = $xml->createElement("items");
  
  $xmlCTitle = $xml->createElement("title","VotePetition");
  $xmlChannel->appendChild($xmlCTitle);
  
  $xmlCLink = $xml->createElement("link","911Thai.com");
  $xmlChannel->appendChild($xmlCLink);
  
  $xmlDate = $xml->createElement("pubDate",date("Y-m-d H:i:s", time()));
  $xmlChannel->appendChild($xmlDate);
  
  $xmlTotalItem = $xml->createElement("totalItems",$totalItems);
  $xmlChannel->appendChild($xmlTotalItem);
    
  $xmlTotalPages = $xml->createElement("totalPages",$totalPages);
  $xmlChannel->appendChild($xmlTotalPages);
  
  $xmlPageSize= $xml->createElement("pageSize",$pagesize);
  $xmlChannel->appendChild($xmlPageSize);
  
  if($nowPage<1)
  $nowPage = 1;

  if(($nowPage>0)&&($nowPage<=$totalPages))
  {
    if($nowPage==1||($nowPage>$totalPages))
    {
      $previousPage = '';
    }else{
      $previousPage = htmlentities(str_replace(basename($_SERVER['PHP_SELF']),"","http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']).'index.php?module=VotePetition&type=xml&func=list&page='.($nowPage-1).$args);
    }

    if(($nowPage>=$totalPages))
    {
      $nextPage = '';
    }else{
      $nextPage = htmlentities(str_replace(basename($_SERVER['PHP_SELF']),"","http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']).'index.php?module=VotePetition&type=xml&func=list&page='.($nowPage+1).$args);
    }
    
  }
  
  $xmlNowPage = $xml->createElement("nowPage",$nowPage);
  $xmlChannel->appendChild($xmlNowPage);
  
  $xmlPreviousPage = $xml->createElement("previousPage",$previousPage);
  $xmlChannel->appendChild($xmlPreviousPage);
  
  $xmlNextPage = $xml->createElement("nextPage",$nextPage);
  $xmlChannel->appendChild($xmlNextPage);
  
  foreach($objDataArray as $val)
  {
    
    $xmlChild = $xml->createElement("item");

    $xmlTitle = $xml->createElement("title",$val['title']);
    $xmlChild->appendChild($xmlTitle);
    
    $xmlHouse = $xml->createElement("house",$val['house_no']);
    $xmlChild->appendChild($xmlHouse);
    
    $xmlLat = $xml->createElement("latitude",$val['latitude']);
    $xmlChild->appendChild($xmlLat);
    $xmlLon = $xml->createElement("longitude",$val['longitude']);
    $xmlChild->appendChild($xmlLon);
    
    $xmlDes = $xml->createElement("description",$val['comment']);
    $xmlChild->appendChild($xmlDes);
    
    $xmlGuid = $xml->createElement("guid",$val['id']);
    $xmlChild->appendChild($xmlGuid);
    
    $xmlDate = $xml->createElement("pubDate",$val['lu_date']);
    $xmlChild->appendChild($xmlDate);
    
    $xmlChannel->appendChild($xmlChild);
    
    if($last<$val['lu_date'])
    $last=$val['lu_date'];
  }

  $xmlUpdateTime = $xml->createElement("updateTime",$last);
  $xmlChannel->appendChild($xmlUpdateTime);
  
  $xml->appendChild($xmlChannel);
  
  
  print $xml->saveXML();
  exit;
  
}

